#!/bin/bash

MPVIPC="$HOME/.mpv/socket"

mkdir -p ~/.mpv
rm -f "$MPVIPC"
mkfifo "$MPVIPC"

command() {
    # Taken from: https://codegists.com/code/mpvctl/
    # thank you
    
    # JSON preamble.
    local tosend='{ "command": ['
    # adding in the parameters.
    for arg in "$@"; do
        tosend="$tosend \"$arg\","
    done
    # closing it up.
    tosend=${tosend%?}' ] }'
    # send it along and ignore output.
    # to print output just remove the redirection to /dev/null
    echo $tosend | socat - "$MPVIPC" &> /dev/null
}

echo getting info

FFINFO=`ffmpeg -i "$1" -vcodec copy -acodec copy -f null dev/null 2>&1`
echo -n .
TIME=`echo "$FFINFO" | grep -i duration | sed -ne 's/.*uration:\([^,]*\),.*/\1/p'`
echo -n .
RES=`echo "$FFINFO" | grep -i stream | grep -i video | sed -ne 's/.*, \([0-9]*x[0-9]*\).*/\1/p' | head -n1`
echo -n .
SIZE=`du -h "$1" | awk -e '{print $1}'`
echo -n .

echo snapping

mkdir -p "/tmp/shots/"

mpv "--input-ipc-server=$MPVIPC" "$1" &
sleep 0.1
command 'set' 'pause' 'yes'
command 'seek' '2' 'absolute-percent'
sleep 0.1
command 'screenshot-to-file' '/tmp/shots/1.png'
command 'seek' '10' 'absolute-percent'
sleep 0.1
command 'screenshot-to-file' '/tmp/shots/2.png'
command 'seek' '20' 'absolute-percent'
sleep 0.1
command 'screenshot-to-file' '/tmp/shots/3.png'
command 'seek' '30' 'absolute-percent'
sleep 0.1
command 'screenshot-to-file' '/tmp/shots/4.png'
command 'seek' '40' 'absolute-percent'
sleep 0.1
command 'screenshot-to-file' '/tmp/shots/5.png'
command 'seek' '50' 'absolute-percent'
sleep 0.1
command 'screenshot-to-file' '/tmp/shots/6.png'
command 'seek' '60' 'absolute-percent'
sleep 0.1
command 'screenshot-to-file' '/tmp/shots/7.png'
command 'seek' '70' 'absolute-percent'
sleep 0.1
command 'screenshot-to-file' '/tmp/shots/8.png'
command 'seek' '80' 'absolute-percent'
sleep 0.1
command 'screenshot-to-file' '/tmp/shots/9.png'
command 'seek' '90' 'absolute-percent'
sleep 0.1
command 'screenshot-to-file' '/tmp/shots/10.png'
command 'quit'

echo -n . ; convert '/tmp/shots/2.png' -resize 50% '/tmp/shots/2h.png'
echo -n . ; convert '/tmp/shots/3.png' -resize 50% '/tmp/shots/3h.png'
echo -n . ; convert '/tmp/shots/4.png' -resize 50% '/tmp/shots/4h.png'
echo -n . ; convert '/tmp/shots/5.png' -resize 50% '/tmp/shots/5h.png'
echo -n '%' ; montage '/tmp/shots/2h.png' '/tmp/shots/3h.png' '/tmp/shots/4h.png' '/tmp/shots/5h.png' -geometry +0+0 '/tmp/shots/q2.png'

echo -n . ; convert '/tmp/shots/6.png' -resize 50% '/tmp/shots/6h.png'
echo -n . ; convert '/tmp/shots/7.png' -resize 50% '/tmp/shots/7h.png'
echo -n . ; convert '/tmp/shots/8.png' -resize 50% '/tmp/shots/8h.png'
echo -n . ; convert '/tmp/shots/9.png' -resize 50% '/tmp/shots/9h.png'
echo -n '*' ; montage '/tmp/shots/6h.png' '/tmp/shots/7h.png' '/tmp/shots/8h.png' '/tmp/shots/9h.png' -geometry +0+0 '/tmp/shots/q3.png'

echo -n + ; montage '/tmp/shots/1.png' '/tmp/shots/q2.png' '/tmp/shots/q3.png' '/tmp/shots/10.png' -geometry +0+0 "/tmp/shots/bigsheet.png"

echo -n - ; convert "/tmp/shots/bigsheet.png" -resize 1280x720\> "/tmp/shots/sheet.png"

echo -n ! ; convert "/tmp/shots/sheet.png" -font "/usr/share/fonts/TTF/DejaVuSans.ttf" -pointsize 30 label:"Duration: $TIME       Resolution: $RES       File Size: $SIZE" -gravity Center -append "/tmp/shots/sheet.next.png"
echo -n . ; mv "/tmp/shots/sheet.next.png" "/tmp/shots/sheet.png"
echo -n . ; cp "/tmp/shots/sheet.png" "$1.sheet.png"
echo
